using System;
using System.Reflection;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using ipi.MCC.Controls;
using ipi.MCC.Global;

namespace ipi.MCC.WebParts
{
    class MCCTreeViewToolPart : Microsoft.SharePoint.WebPartPages.ToolPart
    {
        protected MCCTreeView wpParent;
        protected MCCTreeStartPositionControl ucTree;

        public MCCTreeViewToolPart()
        {            
            this.Title = Resources.Resources.MCCTreeViewToolPart_Title;
            this.Init += new EventHandler(MCCTreeViewToolPart_Init);            
        }

        void MCCTreeViewToolPart_Init(object sender, EventArgs e)
        {
            
            
        }
        
        protected override void CreateChildControls()
        {
            try
            {
                base.CreateChildControls();
                wpParent = (MCCTreeView)this.ParentToolPane.SelectedWebPart;
                ucTree = (MCCTreeStartPositionControl)this.Page.LoadControl(@"/_controltemplates/MCC/MCCTreeStartPositionControl.ascx");            
                ucTree.PreSelectNode(wpParent.StartNodeID);              
                this.Controls.Add(ucTree);
                               
            }
            catch (Exception exc)
            {
                Exceptions.HandleException(exc, this.GetType(), MethodBase.GetCurrentMethod(), LoggingType.Global_Exception);
            }
        }
        
        protected override void RenderToolPart(HtmlTextWriter output)
        {
            EnsureChildControls();

            // The appropriate child control will render based on what was added to the collection
            try
            {
                RenderChildren(output);
            }
            catch (System.Exception exc)
            {
                output.WriteLine("The following error occurred in this Web Part.  Please contact your administrator for further assistance. \n" + Exceptions.HandleException(exc, this.GetType(), MethodBase.GetCurrentMethod(), LoggingType.Global_Exception));
            }
        }
        
        public override void ApplyChanges()
        {
            wpParent = (MCCTreeView)this.ParentToolPane.SelectedWebPart;
            wpParent.StartNodeID = ucTree.GetStartNodeID().ToString();
            wpParent.ForceRefresh();
        }
    }
}
